package com.maoshi.shop.distribution.api.controller;

import cn.binarywang.wx.miniapp.api.WxMaService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.maoshi.shop.delivery.UUdelivery.HttpRequestHelper;
import com.maoshi.shop.distribution.comment.service.DistributionUserService;
import com.maoshi.shop.mp.comment.uril.WxMpaccesstoken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;

/**
 * @author LGH
 */
@RestController
@RequestMapping("/p/distribution/qrCode")
@Api(tags="生成二维码接口")
@AllArgsConstructor
public class DistributionQrCodeController {

	private WxMaService wxMaService;

    private DistributionUserService distributionUserService;
    private WxMpaccesstoken wxMpaccesstoken;


//   @GetMapping("/invitation")
//    public String wxCodeCreate(String page,String scene) throws WxErrorException {
//        String url="https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token="+wxMpaccesstoken.getAccesstoken();
//        String data="{\"expire_seconds\": 604800, \"action_name\": \"QR_STR_SCENE\", \"action_info\": {\"scene\": {\"scene_str\": \""+scene+"\"}}}";
//        String s = HttpRequestHelper.HttpPost(url, data);
//        System.out.println(s);
//        JSONObject jsonObject = JSON.parseObject(s);
//        String ticket = jsonObject.get("ticket").toString();
//        return "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket="+ticket;
//
//    }
@GetMapping("/invitation")
public String wxCodeCreate(String page,String scene) throws WxErrorException {
    String url="https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token="+wxMpaccesstoken.getAccesstoken();
    String data="{\"expire_seconds\": 604800, \"action_name\": \"QR_STR_SCENE\", \"action_info\": {\"scene\": {\"scene_str\": \""+scene+"\"}}}";
    String s = HttpRequestHelper.HttpPost(url, data);
    System.out.println(s);
    JSONObject jsonObject = JSON.parseObject(s);
    String ticket = jsonObject.get("ticket").toString();
    return "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket="+ticket;

}

}
